/*
 * Created on Jun 13, 2004
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License");  you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * http//www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 *
 * The Original Code is MAPONTO.
 *
 * The Initial Developer of the Original Code is University of Toronto. Portions
 * created by University of Toronto are Copyright (C) 2004.  All Rights Reserved.
 *
 * MAPONTO was developed by Knowledge Management Lab
 * (http//www.cs.toronto.edu/km) at the University of Toronto Department of 
 * Computer Science. Current information about MAPONTO can be obtained at 
 * http//resi.ai.toronto.edu:8080
 *
 */

/*
 * Contributor(s): Yuan An yuana@cs.toronto.edu
 */
package main.graph.tests;

import java.io.File;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.apache.log4j.Logger;

import y.base.Edge;
import y.base.EdgeCursor;
import y.base.EdgeList;
import y.base.EdgeMap;
import y.base.Node;

import main.graph.GetCMGraph;
import main.graph.GraphAlgo;
import main.graph.ValuedGraph;
import main.logger.LoggerSetup;
import main.model.Entity;
import main.model.OntoEle;
import main.model.Relationship;
import main.naiveBayes.FindCorrespondence;
import main.naiveBayes.LearnParameter;
import edu.stanford.smi.protegex.owl.ProtegeOWL;
import edu.stanford.smi.protegex.owl.model.OWLIndividual;
import edu.stanford.smi.protegex.owl.model.OWLModel;
import edu.stanford.smi.protegex.owl.model.OWLNamedClass;

public class GetCMGraphTests extends TestCase {

	static {
		LoggerSetup.enable(); // Enable log4j logging
	}

	static Logger log = Logger.getLogger(GetCMGraphTests.class);

	/**
	 * Constructor
	 * 
	 * @param name
	 */
	public GetCMGraphTests(String name) {
		super(name);

	}

	/**
	 * Set-up is always called first by the JUnit test framework.
	 */
	protected void setUp() throws Exception {
		super.setUp();

	}

	/**
	 * NOTE: Tests are identified by JUnit by methods starting with the name
	 * test. The order that JUnit executes test cases is to call, sequentially:
	 * 
	 * setUp() testXXXXX() // test 1 testXXXXX() // test 2 tearDown()
	 * 
	 */

	public void testGetOWL() {
		try {

			File file = new File("ontologies/Bibliographic-Data.owl");

			OWLModel owlModel = ProtegeOWL.createJenaOWLModelFromReader(new FileReader(file));

			Collection classes = owlModel.getUserDefinedOWLNamedClasses();
			for (Iterator it = classes.iterator(); it.hasNext();) {
				OWLNamedClass cls = (OWLNamedClass) it.next();
				Collection instances = cls.getInstances(false);
				System.out.println("Class " + cls.getBrowserText() + " ("
						+ instances.size() + ")");
				for (Iterator jt = instances.iterator(); jt.hasNext();) {
					OWLIndividual individual = (OWLIndividual) jt.next();
					System.out.println(" - " + individual.getBrowserText());
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void testGetGraph() {
		try {

			//File file = new File("Bibliographic-Data.owl");
			
			//File file = new File("factbook-ont.owl");
			
			File file = new File("ontologies/realEstate-v1.owl");
			
			ValuedGraph cmg = GetCMGraph.getGraph(file);
			
			PrintWriter output = new PrintWriter(new File("ontologies/junit-test-CM.output"));
			
			output.println(GetCMGraph.displayCMGraph(cmg));
			output.flush();
			
			System.out.println(GetCMGraph.displayCMGraph(cmg));
			
			/*
			System.out.println("========Get Elements by Label===========");
			
			ArrayList eles = (ArrayList) cmg.getElementsByLabel("ontology:Actor");
			
			for(int i = 0; i < eles.size(); i++){
				Object ele = eles.get(i);
				if(ele instanceof Node){
					Entity entity = (Entity) cmg.getNodeValue((Node) ele);
					System.out.println(entity.display());	
				}
				if(ele instanceof Edge){
					Relationship rel = (Relationship) cmg.getEdgeValue((Edge)ele);
					System.out.println(rel.display());
				}
				
			}
			*/

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void testGetCorrespondences() {
		try {
			
			File file = new File("ontologies/ns-v1.owl");
			
			ValuedGraph cmg = GetCMGraph.getGraph(file);
			
			//PrintWriter output = new PrintWriter(new File("ontologies/junit-test-CM.output"));
			
			System.out.println("========Get Elements by Label===========");
			
			ArrayList<String> labels =  new ArrayList<String>();
			
			labels.add("vso:Automobile");
			labels.add("vso:productionDate");
			labels.add("gr:hasMakeAndModel");
			labels.add("gr:PriceSpecification");
			labels.add("vso:mileageFromOdometer");
			labels.add("gr:Location");
			
			ArrayList<ArrayList<Node>> fixedSet = FindCorrespondence.getFixedNodes(cmg, labels);
			
			int i = 0;
			for(ArrayList<Node> fixed: fixedSet){
				i++;
				
				System.out.println("************A Set of Fixed Nodes: " + i + " *************");
				for(Node node: fixed){
					
					OntoEle ent = (OntoEle) cmg.getNodeValue(node);
					System.out.println("[" + ent.getName() + "]");
				}
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public void testGetSteiners() {
		try {
			
			File file = new File("ontologies/ns-v1.owl");
			
			ValuedGraph cmg = GetCMGraph.getGraph(file);
			
			EdgeMap weights = cmg.createEdgeMap();
			for(EdgeCursor ec = cmg.edges(); ec.ok(); ec.next()){
				Edge edge = ec.edge();
				weights.setDouble(edge, 1.0);
			}
			
			PrintWriter output = new PrintWriter(new File("ontologies/junit-test-Steiners.output"));
			
			System.out.println("========Get Elements by Label===========");
			output.println("========Get Elements by Label===========");
			
			ArrayList<String> labels =  new ArrayList<String>();
			
			labels.add("vso:Automobile");
			labels.add("vso:productionDate");
			labels.add("gr:hasMakeAndModel");
			labels.add("gr:PriceSpecification");
			labels.add("vso:mileageFromOdometer");
			labels.add("gr:Location");
			
			ArrayList<ArrayList<Node>> fixedSet = FindCorrespondence.getFixedNodes(cmg, labels);
			
			int i = 0;
			for(ArrayList<Node> fixed: fixedSet){
				i++;
				
				System.out.println("************A Set of Fixed Nodes: " + i + " *************");
				output.println("************A Set of Fixed Nodes: " + i + " *************");
				for(Node node: fixed){
					
					OntoEle ent = (OntoEle) cmg.getNodeValue(node);
					System.out.println("[" + ent.getName() + "]");
					output.println("[" + ent.getName() + "]");
				}
				
				
				ArrayList<EdgeList> steiners = GraphAlgo.allSteinerTreesIntegrated(cmg, weights, fixed);
				
				System.out.println("&&&&&&&&&&&&&&&&A Set of Steiner Trees&&&&&&&&&&&&&&&&&&&");
				output.println("&&&&&&&&&&&&&&&&A Set of Steiner Trees&&&&&&&&&&&&&&&&&&&");
				for(int j = 0; j < steiners.size(); j++){
					System.out.println("<<<<<<<<<<<The "+ j +"th Steiner for the " + i + "th fixed set:>>>>>>");
					output.println("<<<<<<<<<<<The "+ j +"th Steiner for the " + i + "th fixed set:>>>>>>");
					
					EdgeList amst = steiners.get(j);
					
					for(EdgeCursor amstEc = amst.edges(); amstEc.ok(); amstEc.next()){
						
						Edge e = amstEc.edge();
					
						OntoEle value = (OntoEle) cmg.getEdgeValue(e);
						
						System.out.println(value.display());
						output.println(value.display());
					}
					System.out.println("<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>");
					output.println("<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>");
				}
				System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
				output.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
				
			}
			
			output.flush();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	
	public static Test suite() {
		TestSuite suite = new TestSuite();

		//suite.addTest(new GetCMGraphTests("testGetOWL"));
		
		suite.addTest(new GetCMGraphTests("testGetGraph"));
		
		//suite.addTest(new GetCMGraphTests("testGetCorrespondences"));
		
		//suite.addTest(new GetCMGraphTests("testGetSteiners"));

		return suite;
	}

	/**
	 * tearDown() (does nothing)
	 */
	protected void tearDown() throws Exception {
		super.tearDown();
	}

	/**
	 * The main routine
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		junit.textui.TestRunner.run(suite());
	}
}
